package io.renren.modules.dataInitialization.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.dataInitialization.entity.BinzhiCourseInfoEntity;
import io.renren.modules.dataInitialization.service.BinzhiCourseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("BinzhiCourseInfo/")
public class BinzhiCourseInfoController {

    @Autowired
    BinzhiCourseInfoService binzhiCourseInfoService;


    @RequestMapping("Select")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<BinzhiCourseInfoEntity> cse = binzhiCourseInfoService.queryList(query);
        int total = binzhiCourseInfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(cse, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }
    @RequestMapping("/Delete/{className}/{courseId}")
    public R delete(@PathVariable("className") String className,
                    @PathVariable("courseId") String courseId){
        binzhiCourseInfoService.deleteUnique(className,courseId);

        return R.ok();
    }
    @RequestMapping("/Insert/{year}/{term}")
    public R save(@RequestBody BinzhiCourseInfoEntity ste,
                  @PathVariable("year")String year,
                  @PathVariable("term")String term){
        binzhiCourseInfoService.save(ste,year,term);
        return R.ok();
    }
    @RequestMapping("/Info/{className}/{courseId}")
    public R info(@PathVariable("className") String className,
                  @PathVariable("courseId") String courseId){
        BinzhiCourseInfoEntity cse = binzhiCourseInfoService.queryUnique(className,courseId);
        return R.ok().put("BinzhiCourseInfo", cse);
    }
    @RequestMapping("/Update/{year}/{term}")
    public R update(@RequestBody BinzhiCourseInfoEntity cse,
                    @PathVariable("year")String year,
                    @PathVariable("term")String term){
        binzhiCourseInfoService.update(cse,year,term);
        return R.ok();
    }
    @RequestMapping("/ExcelImport/{year}/{term}")
    public R upload(@RequestParam("file") MultipartFile file,
                    @PathVariable("year")String year,
                    @PathVariable("term")String term) throws Exception {
        //这里只写了elx一种，就是excel2003-2007这个版本的， 别用错了
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        try{
            String []years=year.split("-");
            int startYear=Integer.parseInt(years[0]);
            int endYear=Integer.parseInt(years[1]);
            int termNum=Integer.parseInt(term);
            if(years==null||endYear-startYear!=1){
                throw new RRException("请输入正确的学年");
            }
            if(termNum!=1&&termNum!=2){
                throw new RRException("请输入正确的学期");
            }
        } catch (Exception e){
                throw new RRException("请输入正确的学期学年");
        }

       binzhiCourseInfoService.insertIntoDataBase(file.getInputStream(),year,term);
        return R.ok().put("msg", "数据导入成功");
    }
}